package main.singleton;

/**
 * @Author: SerMs
 * @Date: 2024/06/17 17 17
 * @Email: 1839928782@qq.com
 * @Blog: https://serms.top
 * 线程安全的懒汉模式
 **/
public class Singleton04 {
    private static Singleton04 INSTANCE;

    private Singleton04() {
    }

    // 锁的力度太大了,会导致性能问题
    public static synchronized Singleton04 getInstance() {
        if (INSTANCE == null) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            INSTANCE = new Singleton04();
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            // 多线程
            new Thread(() -> System.out.println(Singleton04.getInstance().hashCode())).start();
        }
    }
}
